package com.ymt.bpm.web.rest.platform.config;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.platform.config.BoService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/10/23.
 */
@RestController
@RequestMapping("/api/v1/platform/config/bo")
public class BoResource {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BoService bos;

    @GetMapping("/query")
    public JSONObject query(HttpServletRequest req,
                            @RequestParam(name="appId", required = false)String appId,
                            @RequestParam(name="search", required = false)String search,
                            @RequestParam(name="sort", required = false)String sort,
                            @RequestParam(name="order", required = false)String order,
                            @RequestParam(name="purpose", required = false)String purpose,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return bos.query(tenantId, appId, purpose, search, sort, order, offset, limit, timezoneOffset);
    }

    @GetMapping("/{boid}")
    public JSONObject get(HttpServletRequest req,
                          @PathVariable(name="boid")String boid,
                          @RequestParam(name="timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get boid=" + boid);
            log.debug("get lang=" + lang);
            log.debug("get timezoneOffset=" + timezoneOffset);
        }
        return bos.get(tenantId, boid, lang, timezoneOffset);
    }

    @GetMapping("/getByName/{boname}")
    public String getByName(HttpServletRequest req,
                            @PathVariable("boname") String boname,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        if (log.isDebugEnabled()) {
            log.debug("getByName boname="+boname);
            log.debug("getByName timezoneOffset="+timezoneOffset);
        }
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String lang = userDetail.get(Const.LANG);
        String tenantId = userDetail.get(Const.TENANT_ID);
        return bos.getByName(tenantId, boname, lang, timezoneOffset);
    }

    @PostMapping("/save")
    public String save(HttpServletRequest req,
                       @RequestParam("bostr") String bostr) {
        if (log.isDebugEnabled()) {
            log.debug("save bostr=" + bostr);
        }
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String lang = userDetail.get(Const.LANG);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tenantId = userDetail.get(Const.TENANT_ID);
        String boid = bos.save(tenantId, bostr, loginName, displayName, lang);
        return "{\"result\":true, \"boid\":\""+boid+"\"}";
    }

    @PostMapping(value="/checkBoname", produces = "plain/text")
    public String checkBoname(HttpServletRequest req,
                              @RequestParam(value="boid", required=false)String boid,
                              @RequestParam("boname")String boname) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkBoname tenantId=" + tenantId);
            log.debug("checkBoname boname=" + boname);
            log.debug("checkBoname boid=" + boid);
        }
        return bos.checkAttr(tenantId, "boname", boname, boid);
    }

    @DeleteMapping("/delete/{boid}")
    public JSONObject delete(HttpServletRequest req,
                             @PathVariable(name="boid")String boid) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
        }
        return bos.delete(tenantId, loginName, displayName, boid);
    }

}
